package controllers

import (
	"net/http"

	"bankend/database"
	"bankend/models"

	"github.com/gin-gonic/gin"
)

// GetPushAudience 根据角色和订阅状态选择消息推送目标
// GET /api/push_audience?role=student&subscribed=true
func GetPushAudience(c *gin.Context) {
	role := c.Query("role")
	subscribedStr := c.Query("subscribed")
	// 默认只选择订阅的用户
	subscribed := true
	if subscribedStr == "false" {
		subscribed = false
	}

	var users []models.User
	db := database.DB
	query := db.Where("deleted_at IS NULL AND subscribed = ?", subscribed)
	if role != "" {
		query = query.Where("role = ?", role)
	}
	if err := query.Find(&users).Error; err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}
	c.JSON(http.StatusOK, gin.H{"users": users})
}
